package com.spider.moudle.handler;

import com.spider.moudle.entity.Setting;
import com.spider.moudle.entity.TaskType;
import com.spider.moudle.factory.ExecutorServiceFactoy;
import com.spider.moudle.factory.NamedThreadFactory;
import com.spider.moudle.producer.StandardProducer;
import com.spider.moudle.util.CenterExecutorUtil;
import com.spider.moudle.util.SettingsUtil;
import com.spider.moudle.worker.StandardWorker;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * Created by Linbo Hu on 2017/11/29.
 * 生产者处理器
 */
public class ProducerHandler {

    /**
     * 生成者处理器方法
     */
    public static void handle() {
        List<Setting> settings = SettingsUtil.getAllSettingList();
        for(Setting setting : settings) {
            Boolean spider_is_worked = setting.getSpider_is_worked();
            if(spider_is_worked) {
                ExecutorService executorService = null;
                try {
                    executorService = ExecutorServiceFactoy.buildExecutorService(1, "生产者", new NamedThreadFactory(setting.getName(), "producer"));
                    executorService.execute(new StandardProducer(setting));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(executorService != null) {
                        executorService.shutdown();
                    }
                }
            } else {
                System.err.println("===  生产者：" + setting.getSite() + " 未加入主线程  ===");
            }
        }
    }
}
